home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / jdirect mouse / source / jdirectmouse.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  2.7 KB  |  115 lines

  1. import java.awt.Point;
  2.  
  3. /**
  4.  * Apple Worldwide Developer Technical Support
  5.  *
  6.  * A simple example of how to obtain the mouse location using the Mac OS Toolbox through JDirect 2.
  7.  *
  8.  * File: JDirectMouse.java
  9.  *
  10.  *
  11.  * @author Levi Brown
  12.  * @author Apple Computer, Inc.
  13.  *
  14.  * Copyright ©1999 Apple Computer, Inc.
  15.  * All rights reserved.
  16.  *
  17.  * @version 1.0
  18.  * 4/15/1999 Shipped as 'JDirectMouse' sample.
  19.  *
  20.  * You may incorporate this sample code into your applications without
  21.  * restriction, though the sample code has been provided "AS IS" and the
  22.  * responsibility for its operation is 100% yours.  However, what you are
  23.  * not permitted to do is to redistribute the source as "Apple Sample
  24.  * Code" after having made changes. If you're going to re-distribute the
  25.  * source, we require that you make it clear in the source that the code
  26.  * was descended from Apple Sample Code, but that you've made changes.
  27.  */
  28. public class JDirectMouse
  29. {
  30.     protected boolean isRun;
  31.     protected Thread thread;
  32.     
  33.     /**
  34.      * The entry point for this program
  35.      */
  36.     static public void main(String args[])
  37.     {
  38.         (new JDirectMouse()).start();
  39.     }
  40.  
  41.     /**
  42.      * Constructs a new JDirectMouse object and initializes defaults.
  43.      */
  44.     public JDirectMouse()
  45.     {
  46.         isRun = false;
  47.         thread = null;
  48.     }
  49.  
  50.     /**
  51.      * Obtains the mouse location by calling the Mac OS toolbox call GetMouse.
  52.      * @return the location of the mouse in coordinates local to the current native grafPort.
  53.      */
  54.     public Point getMouseLocation()
  55.     {
  56.         //Create a new Point structure to be populated by the native call.
  57.         PointStruct mouseLoc = new PointStruct();
  58.         //Call the toolbox for the mouse location
  59.         EventFunctions.GetMouse(mouseLoc);
  60.         //Convert the native stucture to a java.awt.Point object, and return it.
  61.         return new Point (mouseLoc.getH(), mouseLoc.getV());
  62.     }
  63.     
  64.     /**
  65.      * Starts the thread to poll the mouse location and print it out to the console.
  66.      * @see #stop
  67.      */
  68.     public void start()
  69.     {
  70.         if (thread == null || !thread.isAlive())
  71.         {
  72.             thread = new Thread(new Task());
  73.             isRun = true;
  74.             thread.start();
  75.         }
  76.     }
  77.     
  78.     /**
  79.      * Stops the thread from polling the mouse location and printing it out to the console.
  80.      * @see #start
  81.      */
  82.     public void stop()
  83.     {
  84.         isRun = false;
  85.     }
  86.  
  87.     /**
  88.      * An inner class containing the body of the thread to poll the mouse.
  89.      */
  90.     public class Task implements Runnable
  91.     {
  92.         public void run()
  93.         {
  94.             Point oldLoc = new Point();
  95.             Point newLoc;
  96.             
  97.             while (isRun)
  98.             {
  99.                 try
  100.                 {
  101.                     newLoc = getMouseLocation();
  102.                     if ( !oldLoc.equals(newLoc))
  103.                     {
  104.                         oldLoc = newLoc;
  105.                         System.out.println(newLoc.toString());
  106.                     }
  107.                     
  108.                     //Increase time if race conditions occur.
  109.                     Thread.sleep(0);
  110.                 }
  111.                 catch (InterruptedException exc) { }
  112.             }
  113.         }
  114.     }
  115. }